package com.block.channelext.service.channel;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.channelext.model.result.ReportResult;
import com.block.channelext.service.IChannelHandleService;
import com.block.common.service.LogFactory;
import com.block.db.entity.ChannelInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class BianXianMaoFlowChannelHandleService implements IChannelHandleService {

    private String requestUrl = "https://openlog.bianxianmao.com/inads/s.gif";
    private String appkey = "ad_key_100400";
    private String code = "ad_code_90001395";

    private static Logger log = LogFactory.getCommLog();


    @Override
    public ReportResult registerAction(ChannelInfo channelInfo, JSONObject extJson) {

        String bxm_id = extJson.getString("bxm_id");
        if (StrUtil.isEmpty(bxm_id)){
            return ReportResult.newInstance();
        }

        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("mt", "8");
        bizMap.put("bxmid", bxm_id);
        bizMap.put("conversion_type", 2);

        log.info("【变现猫信息流上报】【注册】【开始请求】【reqestUrl】{}【bizMap】{}", requestUrl, JSON.toJSONString(bizMap));
        HttpResponse httpResponse = HttpUtil.createGet(requestUrl)
                .form(bizMap)
                .execute();
        log.info("【变现猫信息流上报】【注册】【开始结束】【result】{}", httpResponse.getStatus());

        Integer status = httpResponse.getStatus();
        String body = httpResponse.body();

        ReportResult reportResult = ReportResult.newInstance(JSON.toJSONString(bizMap), body);
        reportResult.setCode(String.valueOf(status));
        if (ObjectUtil.equal(status, Integer.valueOf(200))){
            // 上报不成功报异常
            reportResult.setSuccess(true);
        }
        return reportResult;
    }

    @Override
    public ReportResult authAction(ChannelInfo channelInfo, JSONObject extJson) {
        return ReportResult.newInstance();
    }

    @Override
    public ReportResult proUvAction(ChannelInfo channelInfo, JSONObject extJson) {
        return ReportResult.newInstance();
    }
}
